<template>
    <div class="content">
      <div class="item" v-show="$store.state.goodsSort === null" v-for="(skeleton,index) in skeletons" :key="index">
        <div style="height:224px;overflow:hidden;">
          <img :src="skeleton.url" alt="图片加载失败">
        </div>
        <div class="text">
          <p style="color:gray;margin:10px 3px 10px 3px;"></p>
        </div>
        <div class="operation">
          <span style="color:#c69c6c;">{{skeleton.title}}</span>
        </div>
      </div>
      <div @click="goDetail(item)" class="item" v-show="$store.state.goodsSort !== null" 
            v-for="item in $store.state.goodsSort" 
            :key="item.goods_id">
        <div style="height:224px;overflow:hidden;">
          <img v-lazy="item.goods_url" alt="图片加载失败">
        </div>
        <div class="text">
          <p style="color:gray;margin:10px 3px 10px 3px;">{{item.goods_title}}</p>
        </div>
        <div class="operation">
          <span style="color:#e5505a;">￥{{item.goods_price}}</span>
          <button @click.stop="addCar(item.goods_id)">购物车</button>
          <button @click.stop="purchase(item)" style="background:#e5505a;">购买</button>
        </div>
      </div>
    <div style="clear:both;"></div>
  </div>
</template>

<script>
import {reqGoodsAll,reqAddCar} from '../api'
export default {
    name:'CommonMall',
    data() {
      return {
        skeletons:[ //用于数据未请求来时渲染一个假dom，撑起屏幕
          {url:require('../assets/lzay.gif'),title:'正在加载。。。'}
        ],
      }
    },
    methods: {
      addCar(id){ //添加到购物车
        if (JSON.parse(localStorage.getItem('key') === null)) {
          this.$message.error('登录令牌不存在！')
          this.$router.push('/user/info')
        }else{
          let token = JSON.parse(localStorage.getItem('key')).token
          if(reqAddCar(id,token).exist === false) {
          this.$message.error('登录令牌失效，请重新登录！')
          this.$router.push('/user/info')
          }else{
            this.$message.success('添加到购物车成功！')
          }
        }
      },
      purchase(item){ //购买
        let token = JSON.parse(localStorage.getItem('key'))
        if (token) {
          item.car_goods_num='1'
          let arr = []
          arr.push(item)
          const array = JSON.stringify(arr)
          this.$router.push({path:'/settlement',query:{value:array}})
        }else{
          this.$message.error('登录令牌不存在！')
          this.$router.push('/user/info')
        }
      },
      goDetail(item){
        const array = JSON.stringify(item)
        this.$router.push({path:'/detail',query:{value:array}})
      }
    },
    mounted() {
      this.$store.commit('goodsSort',reqGoodsAll());
    },
}
</script>

<style scoped>
  /* 每个商品盒子 */
  .item{
    width: 236px;
    float: left;
    border-radius: 8px;
    margin-right: 20px;
    overflow: hidden;
    /* background: #25252B; */
    background: #fff;
    transform: translateY(0);
    /* 移回也需要0.4s时间过渡 */
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .item:hover{
    box-shadow: 0 0 15px 0 gray;
    transform: translateY(-5px);
    /* 0.4s完成transform移动效果*/
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .item:nth-of-type(4n+5){
    margin-right: 0;
  }
  .item:nth-of-type(n+6){
    margin-top: 20px;
  }
  .operation{
    margin: 3px 0 5px 5px;
  }
  .operation button{
    float: right;
    margin-right: 5px;
    margin-bottom: 5px;
  }
  .text{
    /* color: rgba(255, 255, 255, 0.75); */
    font-size: 14px;
    max-width: 100%;
    height: 40px;
    padding:0 5px;
  }
  .text p{
    font-size: 12px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
</style>